package com.yuhuofei2021.spring.boot.weather.config;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.yuhuofei2021.spring.boot.weather.job.WeatherDataSyncJob;

/**
 * @description 用来配置Quartz
 * @author yuhuofei2021
 * @date 2021年9月20日
 */
@Configuration
public class QuartzConfiguration {
	
	//更新频率
	private static final int TIME = 1800;

	/*
	 * 定义一个Quartz需要用到三个对象：
	 * 一个是JobDetail（任务对象），
	 * 一个是Trigger（触发器对象），
	 * 最后一个是Scheduler（定时器对象）
	 */
	@Bean
	public JobDetail weatherDataSyncJobDetail() {
		
		return JobBuilder.newJob(WeatherDataSyncJob.class).withIdentity("weatherDataSyncJob")
				.storeDurably().build();
	}
	
	@Bean
	public Trigger weatherDataSyncTrigger() {
		
		SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
				.withIntervalInSeconds(TIME).repeatForever();
		
		return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail())
				.withIdentity("weatherDataSyncTrigger").withSchedule(scheduleBuilder).build();
	}
}
